//
//  TimelineViewController.m
//  iTimeline
//
//  Created by Mobioneer HV 04 on 1/7/13.
//  Copyright (c) 2013 Mobioneer Co., Ltd. All rights reserved.
//

#import "TimelineViewController.h"
#import "TimeLineLeft.h"
#import "TimeLineRight.h"
#import "TaskData.h"
#import "AppDelegate.h"
@interface TimelineViewController ()

@end



@implementation TimelineViewController
@synthesize tableView = _tableView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    
    
    
    msize = [[NSMutableArray alloc] init];
    [self getData];

 
 
    self.navigationItem.title = @"Timeline";
   
   
    [super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated
{
   
    
    for(int i = 0; i< [data count]; i++)
    {
        TaskData *task = [data objectAtIndex:i];
    _textView.text = task.taskName;
    
        [msize addObject:[NSString stringWithFormat:@"%f",_textView.contentSize.height ]];
 
    }
    [self getData];
    [_tableView reloadData];
   
}



-(void)getData
{
    NSError *error;
    AppDelegate *delegate = [[UIApplication sharedApplication]delegate];
    //Get information
    
    
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"TaskData" inManagedObjectContext:delegate.managedObjectContext];
    [fetchRequest setEntity:entity];
   
    // NSPredicate *predicate =
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
                                        initWithKey:@"time"
                                        ascending:NO];
    
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
    NSArray *tempArr = [delegate.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    
    
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
    [dateFormat setDateFormat:@"dd/MM/yyyy"];
    NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"vi_VN"];
    [dateFormat setLocale:usLocale];
    timeData = [[NSMutableArray alloc] init];
    data = [[NSMutableArray alloc] init];
    NSLog(@"size: %d", [tempArr count]);
    for (TaskData *d in tempArr) 
    {
        [data addObject:d];
       
    }
                   
              
           
            
     
    
    
    
    
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

# pragma - TableView

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [data count];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}



- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(10.0, 0.0, 300.0, 44.0)];
    
    // create the button object
    UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    headerLabel.backgroundColor = [UIColor clearColor];
    headerLabel.opaque = NO;
    headerLabel.textColor = [UIColor redColor];
    headerLabel.highlightedTextColor = [UIColor whiteColor];
    headerLabel.font = [UIFont boldSystemFontOfSize:20];
    headerLabel.frame = CGRectMake(110, -10.0, 300.0, 44.0);
    
    // If you want to align the header text as centered
    // headerLabel.frame = CGRectMake(150.0, 0.0, 300.0, 44.0);
    
    headerLabel.text = @"15/01/2013";
    [customView addSubview:headerLabel];
    
    return customView;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    TaskData *task = [data objectAtIndex:indexPath.row];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    
    [dateFormatter setDateFormat:@"hh:mm"];
    if(indexPath.row % 2 == 0)
    {
         static NSString *cellIdentifier = @"TimeLineLeft";
        TimeLineLeft *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell == nil) {
            cell = [[NSBundle mainBundle] loadNibNamed:cellIdentifier owner:self options:nil][0];
        }
        cell.content.text = task.taskName;
        CGRect frame = cell.content.frame;
        frame.size.height = cell.content.contentSize.height;
        cell.content.frame = frame;
        cell.time.text = [dateFormatter stringFromDate:task.time];
        CGRect frame1 = cell.line.frame;
        frame1.size.height += cell.content.contentSize.height;
        cell.line.frame = frame1;
        return cell;
    }
    else
    {
         static NSString *cellIdentifier = @"TimeLineRight";
        TimeLineRight *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell == nil) {
            cell = [[NSBundle mainBundle] loadNibNamed:cellIdentifier owner:self options:nil][0];
        }
        cell.content.text = task.taskName;
        CGRect frame = cell.content.frame;
        frame.size.height = cell.content.contentSize.height;
      cell.time.text = [dateFormatter stringFromDate:task.time];
        cell.content.frame = frame;
        CGRect frame1 = cell.line.frame;
        frame1.size.height += cell.content.contentSize.height;
        cell.line.frame = frame1;
        return cell;
    }

 
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return [[msize objectAtIndex:indexPath.row] floatValue] + 45;
}

@end
